//
//  Gameover.swift
//  Shooting Gallery
//
//  Created by chen on 2019/12/24.
//  Copyright © 2019 Leonard Chan. All rights reserved.
//

import UIKit
import SpriteKit

class Gameover: SKScene{
    
    var leftButtnoNode: SKSpriteNode!
    var totalScore: Int!
 
   var manager: GameManager!
    
    override func didMove(to view: SKView) {
        self.leftButtnoNode = SKSpriteNode(texture: SKTexture(imageNamed: "return"))
        self.leftButtnoNode.position = CGPoint(x: 170, y: 300)
        self.leftButtnoNode.zPosition = 12
        self.leftButtnoNode.setScale(1.1)
        self.leftButtnoNode.name = "leftButton"
        self.addChild(self.leftButtnoNode)
        
      
        let restat = SKSpriteNode(texture: SKTexture(imageNamed: "StartAllOverAgain"))
        restat.position = CGPoint(x: 406, y: 165)
        restat.zPosition = 12
        restat.name = "restart"
        self.addChild(restat)
        
        let showStr: SKLabelNode = SKLabelNode.init(text: "Game Over")
        showStr.fontColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        showStr.fontName = "Impact"
        showStr.fontSize = 40
        showStr.zPosition = 12
        showStr.position = CGPoint(x: 406, y: 250)
        showStr.name = "game over"
        self.addChild(showStr)
        
        let top : SKLabelNode = SKLabelNode.init(text: "The Highest:\(self.getScore(scoreIn: SingleHandler.sharedInstance.totalScore))")
        top.fontName = "PingFangSC-Regular"
        top.fontSize = 25
        top.zPosition = 5
        top.position = CGPoint(x: self.frame.midX, y: showStr.frame.maxY + 15)
        top.name = "game top"
        self.addChild(top)
        
        manager = GameManager(scene: self)
        let current: SKLabelNode = SKLabelNode.init(text: "Total Score:\(String(describing: SingleHandler.sharedInstance.totalScore))")
        current.fontColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        current.fontName = "PingFangSC-Regular"
        current.fontSize = 25
        current.zPosition = 12
        current.position = CGPoint(x: 406, y: 210)
        self.addChild(current)
    }
    
    func getScore(scoreIn :Int) -> Int{
        let handler = CJDPlayerHandler.sharedInstance
        return handler.CJDTopScore(score: scoreIn)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        for touchIn in touches {
            let pt = touchIn.location(in: self.scene!)
            let nodes = self.nodes(at: pt)
            if let node = nodes.first as? SKSpriteNode{
                if node.name == "leftButton"{
                    if let gameStart = SKScene(fileNamed: "GameStart") {
                        gameStart.scaleMode = .aspectFill
                        self.view?.presentScene(gameStart, transition: SKTransition.push(with: .left, duration: 0.3))
                    }
                }else if node.name == "restart"{
                    if let stageScene = SKScene(fileNamed: "StageScene") {
                        
                        stageScene.scaleMode = .aspectFill
                        
                        self.view?.presentScene(stageScene, transition: SKTransition.doorsOpenVertical(withDuration: 1))
                    }
                }
            }
        }
    }
}
